﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Thor.Common;
using Thor.Configuration;
using Thor.Domain;
using Thor.Domain.Configuration;

namespace Thor.Domain
{
    public class SettingsHelper
    {
        #region fields

        private static ConfigurationSettings _ConfigSettings = new ConfigurationSettings();
        private static string _ApplicationNameKey = ConfigurationSettings.ApplicationNameKey;
        private static string _ApplicationNameValue = ConfigurationSettings.ApplicationNameValue;
        private static string _ConnectionStringKey = ConfigurationSettings.ConnectionStringKey;
        private static string _ConnectionStringValue = ConfigurationSettings.ConnectionStringValue;
        private static string _RepositoryAssembly = AppDomain.CurrentDomain.BaseDirectory + ConfigurationSettings.SettingsRepositoryAssembly;
        private static string _RepositoryType = ConfigurationSettings.SettingsRepositoryType;

        #endregion

        #region constructors

        private SettingsHelper() { }

        #endregion

        #region properties

        private static ConfigurationSettings ConfigurationSettings
        {
            get
            {
                if (_ConfigSettings == null)
                    _ConfigSettings = new ConfigurationSettings();

                return _ConfigSettings;
            }
        }

        #endregion

        #region methods

        public static bool AreSettingsConfigured()
        {
            SettingsManager manager = GetManager();
            Settings settings = manager.Settings;

            if (settings == null)
                return false;
            else
            {
                if (string.IsNullOrEmpty(settings.AssemblyName) || string.IsNullOrEmpty(settings.AssemblyType)
                    || string.IsNullOrEmpty(settings.CalendarProviderUri) || string.IsNullOrEmpty(settings.UserName)
                    || string.IsNullOrEmpty(settings.Password))
                    return false;
                else
                    return true;
            }
        }

        public static Settings GetSettings()
        {
            SettingsManager manager = GetManager();
            Settings settings = manager.Settings;

            if (settings != null)
                return settings;
            else
                return new Settings();
        }

        public static NetworkCredential GetNetworkCredential()
        {
            Settings settings = GetSettings();
            string separator = @"\";

            if (settings != null)
            {
                if (settings.UserName.Contains(separator))
                {
                    string[] split = settings.UserName.Split(separator.ToCharArray());
                    return new NetworkCredential(split[1], settings.Password, split[0]);
                }
                else
                    return new NetworkCredential(settings.UserName, settings.Password);
            }
            else
                return null;
        }

        private static Dictionary<string, object> GetRepositoryParameters()
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add(_ApplicationNameKey, _ApplicationNameValue);
            parameters.Add(_ConnectionStringKey, _ConnectionStringValue);
            return parameters;
        }

        private static ISettingsRepository GetRepository(Dictionary<string, object> parameters)
        {
            return SettingsManager.GetRepository(_RepositoryAssembly, _RepositoryType, parameters);
        }

        public static SettingsManager GetManager()
        {
            Dictionary<string, object> parameters = GetRepositoryParameters();
            ISettingsRepository repository = GetRepository(parameters);
            SettingsManager manager = new SettingsManager(repository);
            return manager;
        }

        #endregion
    }
}
